/**
 * @file   ExecuteMethodRequestPacket.cpp
 * @author Djuro Drljaca (djurodrljaca@gmail.com)
 * @date   2014-04-25
 * @brief  DON packet object: Execute Method Request
 *
 * Copyright 2014  Djuro Drljaca (djurodrljaca@gmail.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <DON/Core/Packet/ExecuteMethodRequestPacket.h>
#include <DON/Core/Packet/PacketDefines.h>
#include <DON/Core/DataType/UInt16.h>
#include <QtCore/QByteArray>
#include <QtCore/QList>

#define MAX_METHOD_ARGUMENT_VALUE_COUNT    (DON_CORE_PACKET_MAX_PAYLOAD_DATA_ITEM_COUNT - 1)

using namespace DON::Core::Packet;
using namespace DON::Core::DataType;

class ExecuteMethodRequestPacket::Private
{
public:
    Private()
        : methodId(0u),
          methodArgumentValueList(QList<const AbstractDataType *>())
    { }

    ~Private()
    {
        removeAllMethodArgumentValues();
    }

    void removeAllMethodArgumentValues();

    quint16 methodId;
    QList<const AbstractDataType *> methodArgumentValueList;
};

ExecuteMethodRequestPacket::ExecuteMethodRequestPacket()
    : AbstractPacket(),
      d(new Private())
{
    setPacketType(PacketType_ExecuteMethodRequest);
}

ExecuteMethodRequestPacket::ExecuteMethodRequestPacket(const quint16 source,
                                                       const quint16 destination,
                                                       const quint16 sequence,
                                                       const quint16 methodId)
    : AbstractPacket(source,
                     destination,
                     PacketType_ExecuteMethodRequest,
                     sequence),
      d(new Private())
{
    d->methodId = methodId;
}

ExecuteMethodRequestPacket::ExecuteMethodRequestPacket(
        const ExecuteMethodRequestPacket &other)
    : AbstractPacket(other),
      d(new Private())
{
    d->methodId = other.d->methodId;

    const int size = other.d->methodArgumentValueList.size();

    for (int index = 0; index < size; ++index)
    {
        const AbstractDataType *argumentValue = other.d->methodArgumentValueList.at(index);

        if (argumentValue != NULL)
        {
            d->methodArgumentValueList.append(argumentValue->clone());
        }
    }
}

ExecuteMethodRequestPacket::~ExecuteMethodRequestPacket()
{
    delete d;
}

ExecuteMethodRequestPacket & ExecuteMethodRequestPacket::operator=(
        const ExecuteMethodRequestPacket &other)
{
    AbstractPacket::operator=(other);

    d->methodId = other.d->methodId;

    d->removeAllMethodArgumentValues();

    const int size = other.d->methodArgumentValueList.size();

    for (int index = 0; index < size; ++index)
    {
        const AbstractDataType *argumentValue = other.d->methodArgumentValueList.at(index);

        if (argumentValue != NULL)
        {
            d->methodArgumentValueList.append(argumentValue->clone());
        }
    }

    return *this;
}

bool ExecuteMethodRequestPacket::isValid() const
{
    bool valid = false;

    if (d->methodArgumentValueList.size() <= MAX_METHOD_ARGUMENT_VALUE_COUNT)
    {
        valid = true;
        const int size = d->methodArgumentValueList.size();

        for (int index = 0; index < size; ++index)
        {
            const AbstractDataType *argumentValue = d->methodArgumentValueList.at(index);

            if (argumentValue != NULL)
            {
                valid = isDataTypeValid(argumentValue->getDataType());
            }
            else
            {
                valid = false;
            }

            if (valid == false)
            {
                break;
            }
        }
    }

    return valid;
}

quint16 ExecuteMethodRequestPacket::getMethodId() const
{
    return d->methodId;
}

void ExecuteMethodRequestPacket::setMethodId(const quint16 methodId)
{
    d->methodId = methodId;
}

int ExecuteMethodRequestPacket::getMethodArgumentValueCount() const
{
    return d->methodArgumentValueList.size();
}

const AbstractDataType * ExecuteMethodRequestPacket::getMethodArgumentValue(const int index) const
{
    const AbstractDataType *argumentValue = NULL;

    if ((index >= 0) &&
        (index < d->methodArgumentValueList.size()))
    {
        argumentValue = d->methodArgumentValueList.at(index);
    }

    return argumentValue;
}

bool ExecuteMethodRequestPacket::setMethodArgumentValue(const AbstractDataType &argumentValue,
                                                        const int index)
{
    bool success = false;

    if ((index >= 0) &&
        (index < d->methodArgumentValueList.size()))
    {
        delete d->methodArgumentValueList[index];

        d->methodArgumentValueList[index] = argumentValue.clone();

        if (d->methodArgumentValueList.at(index) != NULL)
        {
            success = true;
        }
    }

    return success;
}

bool ExecuteMethodRequestPacket::addMethodArgumentValue(const AbstractDataType &argumentValue)
{
    bool success = false;

    if (d->methodArgumentValueList.size() < MAX_METHOD_ARGUMENT_VALUE_COUNT)
    {
        d->methodArgumentValueList.append(argumentValue.clone());

        if (d->methodArgumentValueList.last() != NULL)
        {
            success = true;
        }
    }

    return success;
}

bool ExecuteMethodRequestPacket::removeMethodArgumentValue(const int index) const
{
    bool success = false;

    if ((index >= 0) &&
        (index < d->methodArgumentValueList.size()))
    {
        delete d->methodArgumentValueList.takeAt(index);
        success = true;
    }

    return success;
}

void ExecuteMethodRequestPacket::removeAllMethodArgumentValues()
{
    d->removeAllMethodArgumentValues();
}

int ExecuteMethodRequestPacket::getMaxMethodArgumentValueCount()
{
    return MAX_METHOD_ARGUMENT_VALUE_COUNT;
}

QList<QByteArray> ExecuteMethodRequestPacket::getPayloadDataItems(bool *ok) const
{
    QList<QByteArray> payloadDataItems;
    bool success = false;

    if (isValid())
    {
        success = true;

        // Method ID
        payloadDataItems.append(UInt16::toBinary(d->methodId));

        // Argument Value List
        const int size = d->methodArgumentValueList.size();

        for (int index = 0; index < size; ++index)
        {
            // Argument value
            const AbstractDataType *argumentValue = d->methodArgumentValueList.at(index);
            const char dataTypeData = fromDataTypeToBinary(argumentValue->getDataType());
            const QByteArray valueData = argumentValue->toBinary();

            if (valueData.isEmpty())
            {
                // Error
                payloadDataItems.clear();
                success = false;
            }
            else
            {
                // Add argument value to the list
                QByteArray argumentValueData;
                argumentValueData.append(dataTypeData);
                argumentValueData.append(valueData);

                payloadDataItems.append(argumentValueData);
            }
        }
    }

    if (ok != NULL)
    {
        *ok = success;
    }

    return payloadDataItems;
}

void ExecuteMethodRequestPacket::Private::removeAllMethodArgumentValues()
{
    const int size = methodArgumentValueList.size();

    for (int index = 0; index < size; ++index)
    {
        delete methodArgumentValueList.at(index);
    }

    methodArgumentValueList.clear();
}
